/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.AppUser;
import org.springblade.modules.app.service.IAppUserService;
import org.springblade.modules.app.vo.AppUserVO;
import org.springblade.modules.app.wrapper.AppUserWrapper;
import org.springblade.modules.system.service.IDictService;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 客户端用户表 控制器
 *
 * @author Blade
 * @since 2019-04-03
 */
@RestController
@AllArgsConstructor
@RequestMapping("/app/appuser")
@Api(value = "客户端用户表", tags = "客户端用户表接口")
public class AppUserController extends BladeController {

	private IAppUserService appUserService;

	private IDictService dictService;

	@GetMapping("/validAccount")
	public R validAccount(String account, Integer id) {
		AppUser appUser = appUserService.getOne(Wrappers.<AppUser>query().lambda().eq(AppUser::getAccount, account));
		return appUser == null ? R.status(true) : R.fail("账户已经存在");
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入appUser", position = 1)
	public R<AppUserVO> detail(AppUser appUser) {
		AppUser detail = appUserService.getOne(Condition.getQueryWrapper(appUser));
		AppUserWrapper appUserWrapper = new AppUserWrapper(dictService);
		return R.data(appUserWrapper.entityVO(detail));
	}

	/**
	 * 分页 客户端用户表
	 */
	@GetMapping("/list")
	@ApiOperation(value = "分页", notes = "传入appUser", position = 2)
	public R<IPage<AppUserVO>> list(AppUser appUser, Query query) {
		IPage<AppUser> pages = appUserService.page(Condition.getPage(query), Condition.getQueryWrapper(appUser));
		AppUserWrapper appUserWrapper = new AppUserWrapper(dictService);
		return R.data(appUserWrapper.pageVO(pages));
	}

	/**
	 * 自定义分页 客户端用户表
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "传入appUser", position = 3)
	public R<IPage<AppUserVO>> page(AppUserVO appUser, Query query) {
		IPage<AppUserVO> pages = appUserService.selectAppUserPage(Condition.getPage(query), appUser);
		return R.data(pages);
	}

	/**
	 * 新增 客户端用户表
	 */
	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入appUser", position = 4)
	public R save(@Valid @RequestBody AppUser appUser) {
		return R.status(appUserService.save(appUser));
	}

	/**
	 * 修改 客户端用户表
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入appUser", position = 5)
	public R update(@Valid @RequestBody AppUser appUser) {
		return R.status(appUserService.updateById(appUser));
	}

	/**
	 * 新增或修改 客户端用户表
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "新增或修改", notes = "传入appUser", position = 6)
	public R submit(@Valid @RequestBody AppUser appUser) {
		return R.status(appUserService.saveOrUpdate(appUser));
	}


	/**
	 * 删除 客户端用户表
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids", position = 7)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(appUserService.removeByIds(Func.toIntList(ids)));
	}

	@GetMapping("/ban")
	public R ban(String ids) {
		AppUser appUser = new AppUser();
		appUser.setStatus(2);
		return R.status(appUserService.update(appUser, Wrappers.<AppUser>update().lambda().in(AppUser::getId, Func.toIntList(ids))));
	}

	@GetMapping("/unBan")
	public R unBan(String ids) {
		AppUser appUser = new AppUser();
		appUser.setStatus(1);
		Wrappers.update().lambda();
		return R.status(appUserService.update(appUser, Wrappers.<AppUser>update().lambda().in(AppUser::getId, Func.toIntList(ids))));
	}

	public static void main(String[] args) {
		ExpressionParser parser = new SpelExpressionParser();
		Expression exp = parser.parseExpression("'Hello World'");
		String message = (String) exp.getValue();
		System.out.println(message);
	}
}
